var ExpenseDetailPage = {
	'onFindOneSucc': function(data, status, xhr) {
		$("#expenseDetailFrm").autofill(data.info, {
			'triggerPropertyChange': ['isInstalment'],
			'onRenderItemObject': {
				'isCountTime': function(elt, k, v) {
					if (v == '01') {
						var distance = DateUtil.getDaysDistance(data.info.consumeTime);
						var str = '是，距今' + distance +  '天';
						return str;
					}
					return '否';
				}
			}
		});
		if (data.info.isInstalment == '01') {
			// 分期付款
			$("#instalmentDetailFrm").removeAttr("data-hide");
			datatable.refresh();
		}
	},
	'onRenderItem': function(colName,text,indexStr,mItem,rowNumber) {
		switch(colName) {
	    case 'isRepay':
	    	var $isRepayContainer = $("<div/>");
	        var repayBtnId = "isRepay_" + indexStr + "_" + rowNumber;
	        var $isRepayEditBtn = $("<input name='isRepay' id='" + repayBtnId + "' type='checkbox' onchange='ExpenseDetailPage.onIsRepayValueChanged(this)' " +
	        		"data-id='" + mItem.id + "' class='m-single'>");
	        if (text == '01') {
	        	$isRepayEditBtn.prop('checked', true); 
	        }
    		var $isRepayLabel = $("<label for='" + repayBtnId + "'></label>");
	        $isRepayContainer.append($isRepayEditBtn);
	        $isRepayContainer.append($isRepayLabel);
	        return $isRepayContainer;
	    case 'repayAmount':
	    	var $container = $("<div/>");
	    	var id = "repayAmount_" + rowNumber;
	    	var $input = $("<input name='repayAmount' data-tag='repayAmount' type='text' id='" + id + "' data-id='" + mItem.id +"'>");
	    	$input.attr('readonly', 'readonly');
	    	$input.val(text);
	    	var $editBtn = $("<a href='javascript:void(0)' data-tag='editHandleBtn' data-target='" + id + "' onclick='ExpenseDetailPage.editRepayAmount(this)'><i class='fa fa-fw fa-edit'></i></a>");
	    	var $okBtn = $("<a href='javascript:void(0)' class='m-color-dark-green' data-tag='okHandleBtn' data-target='" + id + "' onclick='ExpenseDetailPage.updateRepayAmount(this)' data-hide='true'><i class='fa fa-fw fa-check-circle'></i></a>");
	    	$container.append($input);
	    	$container.append($editBtn);
	    	$container.append($okBtn);
	    	return $container;
	    case 'remark':
	    	var $remarkContainer = $("<div/>");
	    	var remarkId = "remark_" + rowNumber;
	    	var $remarkInput = $("<input name='remark' data-tag='remark' type='text' id='" + remarkId + "' data-id='" + mItem.id +"'>");
	    	$remarkInput.attr('readonly', 'readonly');
	    	$remarkInput.val(text);
	    	var $editRemarkBtn = $("<a href='javascript:void(0)' data-tag='editHandleBtn' data-target='" + remarkId + "' onclick='ExpenseDetailPage.editRemark(this)'><i class='fa fa-fw fa-edit'></i></a>");
	    	var $okRemarkBtn = $("<a href='javascript:void(0)' class='m-color-dark-green' data-tag='okHandleBtn' data-target='" + remarkId + "' onclick='ExpenseDetailPage.updateRemark(this)' data-hide='true'><i class='fa fa-fw fa-check-circle'></i></a>");
	    	$remarkContainer.append($remarkInput);
	    	$remarkContainer.append($editRemarkBtn);
	    	$remarkContainer.append($okRemarkBtn);
	    	return $remarkContainer;
	    case 'realRepayDate':
	    	var $realRepayDateContainer = $("<div/>");
	    	var realRepayDateId = "realRepayDate_" + rowNumber;
	    	var $realRepayDateInput = $("<input name='remark' data-tag='realRepayDate' type='text' id='" + realRepayDateId + "' data-id='" + mItem.id +"'>");
	    	$realRepayDateInput.attr('readonly', 'readonly');
	    	$realRepayDateInput.val(text);
	    	$realRepayDateContainer.append($realRepayDateInput);
	    	
	    	if (mItem.isRepay == '01') {
	    		var $editRealRepayDateBtn = $("<a href='javascript:void(0)' data-tag='editHandleBtn' data-target='" + realRepayDateId + "' onclick='ExpenseDetailPage.editRealRepayDate(this)'><i class='fa fa-fw fa-edit'></i></a>");
	    		$realRepayDateContainer.append($editRealRepayDateBtn);
	    	}
	    	return $realRepayDateContainer;
		}
	},
	'editRepayAmount': function(ele) {
		var targetId = $(ele).data('target');
		$("#" + targetId).removeAttr("readonly");
		$(ele).parent().find("[data-tag='okHandleBtn']").removeAttr("data-hide");
		$(ele).parent().find("[data-tag='editHandleBtn']").attr("data-hide", 'true');
	},
	'updateRepayAmount': function(ele) {
		var targetId = $(ele).data('target');
		$("#" + targetId).attr("readonly", 'readonly');
		$(ele).parent().find("[data-tag='okHandleBtn']").attr("data-hide", 'true');
		$(ele).parent().find("[data-tag='editHandleBtn']").removeAttr("data-hide");
		
		// 更新还款金额
		var repayAmount = $("#" + targetId).val();
		var id = $("#" + targetId).data('id');
		updateRepayAmountAjax.tempUrl(function(url) {
			return url + id;
		}).setData({
			'repayAmount': repayAmount
		}).send();
		
		// ExpenseDetailPage.updateTimeUpdate(ele);
	},
	'editRemark': function(ele) {
		var targetId = $(ele).data('target');
		$("#" + targetId).removeAttr("readonly");
		$(ele).parent().find("[data-tag='okHandleBtn']").removeAttr("data-hide");
		$(ele).parent().find("[data-tag='editHandleBtn']").attr("data-hide", 'true');
	},
	'updateRemark': function(ele) {
		var targetId = $(ele).data('target');
		$("#" + targetId).attr("readonly", 'readonly');
		$(ele).parent().find("[data-tag='okHandleBtn']").attr("data-hide", 'true');
		$(ele).parent().find("[data-tag='editHandleBtn']").removeAttr("data-hide");
		
		// 更新还款金额
		var remark = $("#" + targetId).val();
		var id = $("#" + targetId).data('id');
		updateRemarkAjax.tempUrl(function(url) {
			return url + id;
		}).setData({
			'remark': remark
		}).send();
		
		// ExpenseDetailPage.updateTimeUpdate(ele);
	},
	'editRealRepayDate': function(ele) {
		var targetId = $(ele).data('target');
		var id = $("#" + targetId).data('id');
		MsgBox.open("editRealRepayDate", "修改实际还款日期", ctx + "/expense/realRepayDate/update/" + id + "/index", "300px");
	},
	'onIsRepayValueChanged': function(ele) {
		var id = $(ele).data('id');
		var isRepay = $(ele).prop("checked") ? '01': '02';
		updateRepayStateAjax.tempUrl(function(url) {
			return url + id;
		}).setData({
			'isRepay': isRepay
		}).send();
		
		// ExpenseDetailPage.updateTimeUpdate(ele);
	},
	'updateRemarkSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		datatable.refresh(false);
	},
	'updateRepayAmountSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		datatable.refresh(false);
	},
	'updateRepayStateSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		datatable.refresh(false);
	},
	'updateTimeUpdate': function(ele) {
		var updateTime = (new Date()).pattern('yyyy-MM-dd HH:mm:ss');
		$(ele).parents('tr').find("[column-key='updateTime'] p").text(updateTime);
	},
	// 下载附件
	'download': function(item, e, rowNumber) {
		$.download({
			'data':{
				'fileId':item.fileId,
				'download':true
			},
			'url':ctx + "/datum/file/fileDownload"
		});
		ExpenseDetailPage.updateDownloadCount(item.fileId, rowNumber); // 更新下载次数
	},
	// 通过超链接下载文件，更新下载次数
	'updateDownloadCount':function(fileId, rowNumber) {
		updateDownloadCountAjax.tempUrl(function(url){
			return url + "/" + fileId + "/" + rowNumber;
		});
		updateDownloadCountAjax.send();
	},
	// 更新下载次数成功
	'onUpdateDownloadCountSucc':function(data, status, xhr) {
		var obj = data.info; // obj.fileId, obj.row
		var row = obj.row;
		var $downloadCount = $("#expenseFileDatatable").find("tr[row-index='" + row + "']").find("td[column-key='downloadCount'] p");
		$downloadCount.text(parseInt($downloadCount.text()) + 1);
	},
};